package com.glory.brepda.activity.mfg;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.AddCarrierDataAdapter;
import com.glory.brepda.adapter.EqpUnitAdapter;
import com.glory.brepda.adapter.GroupEqpListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.DefectCodeEntity;
import com.glory.brepda.entity.MultiEqpBean;
import com.glory.brepda.entity.ResponseMessageEntity;
import com.glory.brepda.entity.ScrapEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.entity.stateChangeEntity;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

public class eqpUnitActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpid;
    @BindView(R.id.rvEqpList)
    RecyclerView rvEqpList;

    private EqpUnitAdapter eqpUnitAdapter;





    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_unit;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("绑定");
        eqpUnitAdapter = new EqpUnitAdapter(null);
        initRecyclerView(rvEqpList, this);
        rvEqpList.setAdapter(eqpUnitAdapter);
        initEdit(etEqpid, s->{
            for (String eqpId : eqpUnitAdapter.getData()){
                if (eqpId.equals(s)){
                    showToast("当前设备已添加到列表",false);
                    return;
                }
            }
            eqpUnitAdapter.addData(0,s);
            etEqpid.setText("");
            etEqpid.requestFocus();
        });

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tvBaseSubmit) {
            StringBuffer equipments = new StringBuffer();
            for (String eqpId : eqpUnitAdapter.getData()){
                if (!"".equals(eqpId)){
                    if ("".equals(equipments.toString())){
                        equipments.append(eqpId);
                    }else{
                        equipments.append("|" + eqpId);
                    }
                }
            }
            if ("".equals(equipments)){
                showToast("未扫入设备，无法绑定",false);
                return;
            }
            if(eqpUnitAdapter.getData().size()==1){
                showToast("不允许单设备绑定机组",false);
                return;
            }
            bind(equipments.toString());
        }

    }
    private void bind(String equipments) {
        FreeUI_EntityCallBack<BaseResponseBean<ResponseMessageEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ResponseMessageEntity>>
                (new TypeToken<BaseResponseBean<ResponseMessageEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showConfirmDialog("绑定成功");

                }

            }

            @Override
            public void onError(Response<BaseResponseBean<ResponseMessageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EQPUNIT");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENTS",equipments);
        map.addProperty("ACTIONTYPE","BIND");
        NetUtils.requestPostNet("IF.EQPUNIT","Common", eqpUnitActivity.this,map,callBack);
    }





}
